package com.dm.wallpaper.library.fraction;

import com.dm.wallpaper.library.ResourceTable;
import com.dm.wallpaper.library.abilityPackage.WallpaperBoardApplication;
import com.dm.wallpaper.library.ablility.WallpaperAbility;
import com.dm.wallpaper.library.database.Database;
import com.dm.wallpaper.library.items.Wallpaper;
import com.dm.wallpaper.library.utils.Log;
import com.dm.wallpaper.library.utils.SystemUtils;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;

import java.util.List;

public class FavoritesFraction extends Fraction implements Component.ClickedListener {
    Component component;
    Component childContent;
    DirectionalLayout content_show;
    Image imageEmpty;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        component = scatter.parse(ResourceTable.Layout_fraction_favorite_layout, container, false);
        return component;
    }

    @Override
    protected void onActive() {
        Log.i("FavoritesFraction.onActive([]):43");
        super.onActive();
        component.findComponentById(ResourceTable.Id_left_mune_icon).setClickedListener(this);
        imageEmpty = (Image) component.findComponentById(ResourceTable.Id_image_empty);
        initView();
    }

    private void initView() {
        if (childContent != null && childContent.getComponentParent() != null) {
            content_show.removeComponent(childContent);
        }

        List<Wallpaper> newWallpapers = Database.get().getFavoritesWallpapers();
        if (newWallpapers == null || newWallpapers.size() == 0) {
            //TODO 当前使用静态加载的数据
            newWallpapers = WallpaperBoardApplication.newWallpapers;
        }
        childContent = ContentCompentUtils.initWallpaperView(newWallpapers, getFractionAbility(), false, new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (component.getId() == ResourceTable.Id_favorite_Image) {
                    initView();
                }
            }
        });
        content_show = ((DirectionalLayout) component.findComponentById(ResourceTable.Id_content_show));

        content_show.addComponent(childContent);

        if (newWallpapers.size() == 0) {
            imageEmpty.setVisibility(Component.VISIBLE);
        } else {
            imageEmpty.setVisibility(Component.HIDE);
        }
    }


    @Override
    protected void onBackground() {
        super.onBackground();
        Log.i("FavoritesFraction.onBackground([]):183");
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
        Log.i("FavoritesFraction.onForeground([intent]):189");
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        Log.i("FavoritesFraction.onStart([intent]):195");
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        Log.i("FavoritesFraction.onInactive([]):201");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("FavoritesFraction.onStop([]):207");
    }

    @Override
    protected void onComponentDetach() {
        super.onComponentDetach();
        Log.i("FavoritesFraction.onComponentDetach([]):213");
    }


    @Override
    public void onClick(Component component) {
        if (component.getId() == ResourceTable.Id_left_mune_icon) {
            if (getFractionAbility() instanceof WallpaperAbility) {
                ((WallpaperAbility) getFractionAbility()).showMunePop();
            }
        }
    }
}
